import {PlainCmsNamespace} from "@peryl/react-cms-editor";
import {iResume} from "./resume.type";
import {iResumeRecord} from "./resume.utils";

export function decodeResumeRecord(resumeRecord: iResumeRecord | undefined): { dataMetas: null, resumeInfo: null } | { dataMetas: PlainCmsNamespace.iDataMeta[], resumeInfo: iResume } {
  let result = { dataMetas: null, resumeInfo: null };
  if (!resumeRecord?.jsonString) {
    console.log('init resume: no jsonString');
    return result;
  }
  const _dataMetas: PlainCmsNamespace.iDataMeta[] = JSON.parse(resumeRecord.jsonString);
  const _rootMeta = _dataMetas.find(i => !i.parentId);
  if (!_rootMeta) {
    console.log('init resume: no root meta');
    return result;
  }
  const resumeInfo: iResume | undefined = _rootMeta.binding?.resume.bindingSource.data;
  if (!resumeInfo) {
    console.log('init resume: no resume info');
    return result;
  }
  return { dataMetas: _dataMetas, resumeInfo };
}
